package com.jlt.web.account;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.security.springsecurity.SpringSecurityUtils;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.jlt.entity.account.Dept;
import com.jlt.service.account.DeptManager;
import com.jlt.web.CrudActionSupport;
import com.jlt.web.util.Struts2ActionUtils;

/**
 * 部门管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数. 演示带分页的管理界面.
 * 
 * @author zhoutao
 */
// 定义URL映射对应/account/dept.action
@Namespace("/account")
// 定义名为reload的result重定向到dept.action, 其他result则按照convention默认.
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "dept.action", type = "redirect") })
public class DeptAction extends CrudActionSupport<Dept> {

	private static final long serialVersionUID = 1L;
	private static final String ENTITY_NAME = "部门";
	@Autowired
	private DeptManager deptManager;

	// -- 页面属性 --//
	private Long id;
	private Dept entity;
	private Page<Dept> page = new Page<Dept>(10);// 每页10条记录

	// private List<Long> checkedRoleIds; //页面中钩选的角色id列表

	// -- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public Dept getModel() {
		return entity;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = deptManager.getDept(id);
		} else {
			entity = new Dept();
			entity.setCreatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setCreatedTime(new Date());
			entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
			entity.setUpdatedTime(new Date());
		}
	}

	// -- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		String name = Struts2Utils.getParameter("name");
		if (id == null)
			id = new Long(0);
		page = deptManager.searchDept(page, name, id);
		return SUCCESS;
	}
	public String chooseList() throws Exception {
		//allDeptList = deptManager.get();
		return "chooseList";
	}
	// -- CRUD Action 函数 --//
	public String getChildDepts() throws Exception {
		if (id == null) {
			id = new Long(-1);
		}
		List<Dept> list = deptManager.getChildDepts(id);
		List<Map<String, String>> tree = new ArrayList<Map<String, String>>();
		for (Dept l : list) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("id", l.getId().toString());
			map.put("name", l.getName());
			map.put("path", "account/dept.action");
			map.put("isParent", "true");
			tree.add(map);
		}
		Struts2Utils.renderJson(tree);
		return null;
	}

	@Override
	public String input() throws Exception {
		// checkedRoleIds = entity.getRoleIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		Dept pDept = new Dept();
		pDept.setId(Long.valueOf(Struts2Utils.getParameter("pid")));
		pDept.setName(Struts2Utils.getParameter("pname"));
		entity.setParentDept(pDept);
		entity.setUpdatedBy(SpringSecurityUtils.getCurrentUserName());
		entity.setUpdatedTime();
		deptManager.saveDept(entity);
		Map<String, String> message = new HashMap<String, String>();
		message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.SAVE,
				Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME,Struts2ActionUtils.CLOSE_CURRENT);
		Struts2Utils.renderJson(message);
		return null;
	}

	@Override
	public String delete() throws Exception {
		Map<String, String> message = new HashMap<String, String>();
		try {
			if (deptManager.isHaveChildDept(id)) {
				message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
						Struts2ActionUtils.FAILURE, ENTITY_NAME);
				message.put("message", "该部门下有子部门,请先删除所有子部门后再试!!");
			} else {
				deptManager.deleteDept(id);
				message.put("rel", "rightContent");
				message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
						Struts2ActionUtils.SUCCESSFUL, ENTITY_NAME);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			message = Struts2ActionUtils.getDwzOperateMessage(Struts2ActionUtils.DELETE,
					Struts2ActionUtils.FAILURE, ENTITY_NAME);
		}
		Struts2Utils.renderJson(message);
		return null;
	}

	// -- 页面属性访问函数 --//
	/**
	 * list页面显示部门分页列表.
	 */
	public Page<Dept> getPage() {
		return page;
	}

}
